package com.xmut.xmutbackend.handler;

import com.xmut.xmutbackend.service.AIService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(AIService.AIException.class)
    public ResponseEntity<Map<String, Object>> handleAIException(AIService.AIException ex) {
        return ResponseEntity.status(ex.getStatusCode()).body(Map.of(
                "success", false,
                "error", ex.getMessage(),
                "code", ex.getStatusCode()
        ));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Map<String, Object>> handleGlobal(Exception ex) {
        return ResponseEntity.status(500).body(Map.of(
                "success", false,
                "error", "系统错误: " + ex.getMessage(),
                "code", 500
        ));
    }
}
